//
//  AnchorPoint2ViewController.m
//  CoreAnimation
//
//  Created by 张国文 on 16/10/9.
//  Copyright © 2016年 hangzhouyijiyin. All rights reserved.
//

#import "AnchorPoint2ViewController.h"

@interface AnchorPoint2ViewController ()

@property (weak, nonatomic) IBOutlet UIImageView *horologeImageV;// 钟表
@property (weak, nonatomic) IBOutlet UIImageView *hourImageV; // 小时
@property (weak, nonatomic) IBOutlet UIImageView *minuteImageV; // 分钟
@property (weak, nonatomic) IBOutlet UIImageView *secondImageV; // 秒
@property (nonatomic, strong) NSTimer *time;

@end

@implementation AnchorPoint2ViewController

- (void)viewWillDisappear:(BOOL)animated
{
    [super viewWillDisappear:animated];
    [self.time invalidate];
}



- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view from its nib.
    
      // 自己对锚点的认识：layer的锚点和position(图层的中心点)默认是重合的，锚点默认为(0.5,0.5)，不管锚点怎么改变，position都是不会改变的，因此锚点改变其实是将视图的新的锚点的位置重新定位到position上
    self.view.backgroundColor = [UIColor whiteColor];
    self.time = [NSTimer scheduledTimerWithTimeInterval:1 target:self selector:@selector(goTime:) userInfo:nil repeats:YES];
    [self.time fire];
    // changeAnchorPoint
    
    
    self.horologeImageV.image = [UIImage imageWithName:@"horologe"];
    self.hourImageV.image = [UIImage imageWithName:@"hour"];
    self.minuteImageV.image = [UIImage imageWithName:@"minute"];
    self.secondImageV.image = [UIImage imageWithName:@"second"];
    
    [self changeAnchorPoint];
   
}



- (void)goTime:(NSTimer *)timer
{
    NSCalendar *calendar = [[NSCalendar alloc] initWithCalendarIdentifier:NSCalendarIdentifierGregorian];
    NSUInteger units = NSCalendarUnitHour | NSCalendarUnitMinute | NSCalendarUnitSecond;
    NSDateComponents *components = [calendar components:units fromDate:[NSDate date]];
  //  NSLog(@"小时：%f，分钟：%f， 秒：%f", components.hour / 12.0, components.minute / 60.0, components.second / 60.0);
    CGFloat hoursAngle = (components.hour / 12.0) * M_PI * 2.0;
    CGFloat minuteAngel = (components.minute / 60.0) * M_PI * 2.0;
    CGFloat secondAngel = (components.second / 60.0) * M_PI * 2.0;
    self.hourImageV.transform = CGAffineTransformMakeRotation(hoursAngle);
    self.minuteImageV.transform = CGAffineTransformMakeRotation(minuteAngel);
    self.secondImageV.transform = CGAffineTransformMakeRotation(secondAngel);
}



// 改变锚点
- (void)changeAnchorPoint
{
    self.hourImageV.layer.anchorPoint = CGPointMake(0.5, 0.9);
    self.minuteImageV.layer.anchorPoint = CGPointMake(0.5, 0.9);
    self.secondImageV.layer.anchorPoint = CGPointMake(0.5, 0.9);
}

@end
